package oj;

import java.util.HashMap;
import java.util.Map;

public class Num1160 {
    public int countCharacters(String[] words, String chars) {
        Map<Character,Integer> map1=new HashMap<>();
       for(int i=0;i<chars.length();i++){
           map1.put(chars.charAt(i), map1.getOrDefault((chars.charAt(i)),0)+1);
       }
        int count=0;
       for(String word:words) {
           Map<Character, Integer> wordMap = new HashMap<>();
           for (int i = 0; i < word.length(); i++) {
               wordMap.put(word.charAt(i), wordMap.getOrDefault((word.charAt(i)), 0) + 1);
           }

           boolean ans=true;
           for (int i = 0; i < word.length(); i++) {
               if(map1.getOrDefault(word.charAt(i),0)< wordMap.getOrDefault(word.charAt(i),0)){
                   ans=false;
                   break;
               }
           }
           if(ans){
               count+=word.length();
           }
       }
        return count;
    }
}
